package com.jlnx.controller.core;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jlnx.dao.ChainEnterpriseDaoImpl;
import com.jlnx.dao.CoreEnterpriseDaoImpl;
import com.jlnx.dao.EnterpriseDaoImpl;
import com.jlnx.dao.FinanceReqDaoImpl;
import com.jlnx.dao.PayStatusDaoImpl;
import com.jlnx.dao.SupplyChainDaoImpl;
import com.jlnx.pojo.ChainEnterprise;
import com.jlnx.pojo.CoreEnterprise;
import com.jlnx.pojo.Enterprise;
import com.jlnx.pojo.FinanceReq;
import com.jlnx.pojo.SupplyChain;
import com.jlnx.pojo.SupplyChainEnterprise;

/*
 * 展示白名单
 */
@WebServlet("/ShowWhiteListController")
public class ShowWhiteListController extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求信息
		response.setContentType("application/json;charset=UTF-8;text/html");
		//设置请求编码模式
    	request.setCharacterEncoding("utf-8");
				
		 //获取session对象
		 HttpSession hs=request.getSession();
		 //获取登录账号
		 String coreEntNo = ((Enterprise)hs.getAttribute("enterprise")).getEntNo();
		 System.out.println( coreEntNo + ":发起白名单查询请求");
		 
		 SupplyChainDaoImpl supplydao = new SupplyChainDaoImpl();
		 ChainEnterpriseDaoImpl chaindao = new ChainEnterpriseDaoImpl();
		 
		 //获取核心企业相关的全部供应链（暂时先获取一个，后续优化）
		List<SupplyChain> ls = supplydao.selectSupplyChainListByEntNo(coreEntNo);
		System.out.println(ls);
//		for(SupplyChain supplyChain:ls) {
			//获取供应链企业列表
		if(ls.size()!=0) {
		    SupplyChain supplyChain = ls.get(0);
			String supplyList = supplyChain.getMessage();
			String[] array = supplyList.split("\\|");
			
			List<SupplyChainEnterprise> upChain = new ArrayList<SupplyChainEnterprise>();
			List<SupplyChainEnterprise> downChain = new ArrayList<SupplyChainEnterprise>();
			//循环获取企业相关信息
			int flag = 1;//代表上游
			for(int i = 0;i < array.length;i++) {
				
				String entNo = array[i];
				System.out.println("entNo:" + entNo);
				
				SupplyChainEnterprise supplyChainEnterprise = null;
				
				if(entNo.equals(coreEntNo)) {
                    flag++;//经过核心企业标志变为下游					
					continue;
				}
					
				ChainEnterprise chainEnterprise = chaindao.selectByEntNo(entNo);
				if(chainEnterprise != null) {//如果企业不存在
			    	supplyChainEnterprise = new SupplyChainEnterprise(entNo, chainEnterprise.getName(), chainEnterprise.getInstatus(), chainEnterprise.getStatus(), 3);
				}else {
					supplyChainEnterprise = new SupplyChainEnterprise(entNo, "", 0, 0, 3);
				}
				if(flag == 1) {
					upChain.add(supplyChainEnterprise);
				}else {
					downChain.add(supplyChainEnterprise);
				}
			}
				
			request.setAttribute("upChain", upChain);
			request.setAttribute("downChain", downChain);
		}
			
//		}
		System.out.println(ls);
		//发送数据到页面

		//跳转界面
		request.getRequestDispatcher("/main/addWhiteList.jsp").forward(request, response);
				
	}

}
